package com.company.management.controller.report;


import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;

import com.company.management.utils.FileUtils;
import com.company.management.utils.ServletUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Date;

@RestController
@Slf4j
@Api(tags = "汇报模块", value = "FileController")
@RequestMapping("/api")
public class FileController {

    @PostMapping("/upload/file")
    @ApiOperation("汇报上传文件")
    public String uploadFile(@RequestParam("file") MultipartFile file, @RequestParam("type") String type) throws IOException {
        if (file.isEmpty()) {
            return "上传文件失败";
        }
        if (StrUtil.isEmpty(type)) {
            return "参数错误!";
        }
        String date = DateUtil.format(new Date(), "yyyy-MM-dd");
        String fileName = RandomUtil.randomNumbers(10) + "." + FileUtils.getFileExtension(file.getOriginalFilename());
        String path = StrUtil.equals(type, "pic") ? "pic" : "attachment";
        Path targetDir = Paths.get("rc-upload", path, date);
        Files.createDirectories(targetDir);
        Path targetFile = targetDir.resolve(fileName);
        file.transferTo(targetFile);
        return date + "/" + fileName;
    }

    @ApiOperation("获取图片")
    @GetMapping("/pic/get")
    public void getFileUrl(@RequestParam("path") String path1, @RequestParam("filename") String fileName, HttpServletResponse response) throws Exception {
        Path path = Paths.get("rc-upload", "pic", path1, fileName);
        if (!path.toFile().exists()) {
            try {
                response.setStatus(HttpServletResponse.SC_NOT_FOUND);
                response.getWriter().write("文件不存在!");
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
        ServletUtils.fileStreamOut(path, response);
    }

    @ApiOperation("获取头像")
    @GetMapping("/avator/get")
    public void getAvator(@RequestParam("path") String path1, @RequestParam("filename") String fileName, HttpServletResponse response) throws Exception {
        Path path = Paths.get("rc-upload", "avator", path1, fileName);
        if (!path.toFile().exists()) {
            try {
                response.setStatus(HttpServletResponse.SC_NOT_FOUND);
                response.getWriter().write("文件不存在!");
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
        ServletUtils.fileStreamOut(path, response);
    }

    @ApiOperation("获取附件")
    @GetMapping("/attachment/get")
    public void getAttachment(@RequestParam("path") String path1, @RequestParam("filename") String fileName, HttpServletResponse response) throws Exception {
        Path path = Paths.get("rc-upload", "attachment", path1);
        if (!path.toFile().exists()) {
            try {
                response.setStatus(HttpServletResponse.SC_NOT_FOUND);
                response.getWriter().write("文件不存在!");
            } catch (IOException e) {
                log.error(e.getMessage(), e);
            }
        }
        ServletUtils.fileStreamOut(path, response);
    }


}
